define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var queryDate = $("#query_date").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pt/alarm_month/index?query_date=' + queryDate,
                    // add_url: 'pt/alarm_month/add',
                    // edit_url: 'pt/alarm_month/edit',
                    // del_url: 'pt/alarm_month/del',
                    multi_url: 'pt/alarm_month/multi',
                    import_url: 'pt/alarm_month/import',
                    table: 'pt_alarm_month',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'depart.name', title: __('部门')},
                        {field: 'month_fee', title: __('Month_fee'), operate:'BETWEEN'},
                        {field: 'consume_fee', title: __('Consume_fee'), operate:'BETWEEN'},
                        {field: 'percent', title: __('占比月计划'),
                          formatter: function(value, row, index) {
                            if (value >= '50') {
                                return '<font color="red">' + value + '%</font>';
                            }
                            return '<font color="blue">' + value + '%</font>';
                          }
                        },
                        {field: 'plan_total', title: __('Plan_total')},
                        {field: 'consume_total', title: __('Consume_total')},
                        {field: 'surplus_total', title: __('月剩余人次'),
                          formatter: function(value, row, index) {
                              var s = row.plan_total * 1 - row.consume_total * 1;
                              return s;
                          }
                        },
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                search: false,
            });

            $(".btn-embossed").on("click", function() {
                queryDate = $("#query_date").val();
                table.bootstrapTable('refresh', {
                  url: 'pt/alarm_month/index?query_date=' + queryDate
                })
            })
            Form.api.bindevent($("form[role=form]"));
            $(".btn-export").on("click", function() {
                queryDate = $("#query_date").val();
                Fast.api.ajax({
                    url:'pt/alarm_month/export?query_date=' + queryDate
                }, function(res){
                    console.log('export =>', res)
                    window.location.href=res['file']
                }, function(){
                    console.log('导出失败');
                    Backend.api.toastr.error('导出失败');
                });
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
